package sweetguy.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import sweetguy.entity.dto.Room;
import sweetguy.entity.dto.School;

import java.util.List;

@Repository
public interface RoomMapper {
    @Insert("insert into ums_room " +
    "(`title`, `address`, `info`, `capacity`," +
    "`deleted`, `created`, `updated`) " +
    "values (#{title}, #{address}, #{info}, #{capacity}," +
    "#{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Room room);

    @Select("select us.id, us.title, us.address, us.info, us.capacity," +
            "us.deleted, us.created, us.updated from ums_room us " +
            "where us.id = #{param1} and us.deleted = 0")
    Room selectById(Long id);

    @Update("<script>" +
            "update ums_room " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if> " +
            "<if test='address != null'> address = #{address}, </if> " +
            "<if test='info != null'> info = #{info}, </if> " +
            "<if test='capacity != null'> capacity = #{capacity}, </if> " +
            "<if test='deleted != null'> deleted = #{deleted}, </if> " +
            "<if test='created != null'> created = #{created}, </if> " +
            "<if test='updated != null'> updated = #{updated}, </if> " +
            "</set> " +
            "where id = #{id} and deleted = 0" +
            "</script>")
    int updateById(Room room);

    @Update("update ums_room set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Update("<script>" +
            "update ums_room set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
             "${e}" +
             "</foreach>" +
             "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Select("select us.id, us.title, us.address, us.info, us.capacity," +
            "us.deleted, us.created, us.updated from ums_room us where us.deleted = 0")
    List<Room> list();

    @Select("select us.title, " +
            "us.deleted, us.created, us.updated from ums_room us " +
            "where us.title = #{title} " )
    Room selectByAllTitle(Room room);

    @Delete("delete from ums_room where id = #{param1}")
    int deleteById(Long id);
}
